Skip to content

Check enum values against their own enum in typed catches and isOfType - #492

Merged
dimensionscape merged 3 commits into
mainfrom
fix/enum-instance-checks
Nov 27, 2026
Merged

dimensionscape merged 3 commits into
mainfrom
fix/enum-instance-checks

Conversation

@dimensionscape

@dimensionscape dimensionscape commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes #491. For 6.0.2: rebased onto main at f059754, with its Changes.md entries under main's libhxcpp 6.0.2 (unreleased) heading. The third commit fixes what UBSan reported in this pull request's first run, described under "The UBSan report" below.

What was wrong

In compiled code a typed catch of one enum took a value of any enum, and Std.isOfType, is and a safe cast agreed with it:

try {
	throw B.OnlyB;
} catch (e:A) {
	// ran, for a B
} catch (e:B) {}

Std.isOfType(B.OnlyB, A); // true
cast(B.OnlyB, A);         // returned the value

Haxe's interpreter gives the B branch, false, and a throw. cppia had the same fault where the type was a host enum: a script's catch or isOfType against a host enum took any enum value, a script's own included. Script enums were already right.

Cause

The class Haxe 4.3 generates for an enum declares no _hx_ClassId of its own. It inherits EnumBase_obj's, clsIdEnum, and EnumBase_obj::_hx_isInstanceOf answers true for that id on every enum value. A typed catch (IsClass<T>), a safe cast (TCast) and Std.isOfType (TCanCast<T>, through __instanceof) all end in that test, and so does cppia for a host enum. Haxe 5's generator gives each enum its own id (HaxeFoundation/haxe#12151, fixed on development); 4.3's will not change, so the runtime has to answer.

The change

  • HX_DO_ENUM_RTTI gives every generated enum _hx_isEnumValueOf, which compares the value's class with the enum's own.
  • A new hx::IsInstanceOfType<T> in include/hx/Object.h uses that where the type has it and the class id otherwise. CastPtr and TCanCast call it.
  • Classes, hx::EnumBase and Enum keep their old check, and the code generated for class types is unchanged. Enums generated by Haxe 5 take the new path and get the same answer as before.

This changes what compiled programs observe: a value of another enum now passes a typed catch, isOfType and is answer false for it, and a safe cast of it throws. Std.isOfType(v, Enum) and Type.getEnum answer as before, and a value is still an instance of its own enum, type parameters or not. Code built against the old headers, such as a prebuilt static library, keeps the old answer until it is rebuilt.

The UBSan report

The first CI run here passed the core suites on Linux, Windows and macOS, ASan and TSan, and failed UBSan on the new CppiaEnumInstanceChecks:

src/hx/cppia/CppiaClasses.cpp:368:35: runtime error: load of misaligned address 0x762fdd30506c for type 'struct Object *', which requires 8 byte alignment
    #0 hx::createEnum(hx::Class_obj*, String*, int)

That is not the enum check. createEnum is the helper the cppia JIT calls to build a host enum value with arguments. It read them from the script frame in place and wrote the new value back the same way, and a frame keeps 4-byte alignment, so on 64-bit an object slot can sit at an odd multiple of 4. 7a78380 moved the JIT's helpers to copying slots in and out and did not touch this file. The new test is the first to reach the helper at such an offset, with HostB.HostWith(1) in jitted code.

The third commit makes createEnum use JitArg and JitStore, as the other helpers do, and has its own Changes.md line.

There is no UBSan on the machine this was written on, so the UBSan job on this push is that fix's test. What I could run: with a temporary print in createEnum, the test reaches it once, with the frame at 4 modulo 8 and one argument, and with the fix the test's output is unchanged, interpreted and jitted.

Tests

  • test/regression/EnumInstanceChecks: typed catches in both orders and alone, isOfType, is, safe casts, enums with parameters and with type parameters, a value held as Dynamic, and a catch of haxe.Exception, a class or Dynamic after an enum catch. Its 66 lines equal what the interpreter prints.
  • test/regression/CppiaEnumInstanceChecks: script and host enums both ways, interpreted and jitted. Script first, so it needs no line in emscripten-skip.txt.
  • Both fail on main and pass here. IsOfTypeClassEnum passes unchanged.

Run on 64-bit Windows with MSVC and Haxe 4.3.7: the two above, IsOfTypeClassEnum, EnumFormattingShapes, CppiaSafeCasts, CppiaBoolBoxing, the cppia suite interpreted and with -jit (56 assertions each), and test/haxe (2698 assertions).

After the rebase, and again with the third commit: the two new tests, IsOfTypeClassEnum, main's new FloatFromSingle, and the cppia suite interpreted and with -jit (56 assertions each).

Not run here: the whole of test/regression, test/std, the sanitizers, 32-bit and the other platforms.

Cost

For an enum type the check was one virtual call and an integer compare, and is now one virtual call (__GetClass) and a pointer compare. bench/src/EnumBenchmarks.hx gains three benchmarks. Against main, with bench/ci.sh (5 processes of 10 samples):

Benchmark main this branch ratio spread
enum_is_of_type 103M checks/s 99.7M 0.97x ±19.3%
enum_typed_catch 565k throws/s 568k 1.01x ±15.5%
class_typed_catch 581k throws/s 595k 1.02x ±7.2%

Nothing moved past the spread. The two enum benchmarks check the count of checks and throws, not the answers, since main answers them wrongly and stops at the first catch clause.

Nothing in libhxcpp's own code leaned on the old answer: no isOfType or is names an enum type, and the only typed enum catches are of haxe.io.Error in two socket tests, where only that enum arrives.

A generated enum has no class id of its own. Its class inherits
EnumBase_obj's _hx_ClassId, clsIdEnum, and every enum value answers true
to that id. hx::ObjectPtr's cast (CastPtr, in hx/Object.h) and
hx::TCanCast (hx/Class.h) both test the id, so in compiled code a typed
catch of one enum, which Haxe 4 generates as _hx_e.IsClass< ::A >(),
caught a value of any enum, Std.isOfType(v, A), and so v is A, was true
for any enum value through the TCanCast<A_obj> the enum registers with,
and cast(v, A) returned a value of another enum instead of throwing.
cppia asked the same TCanCast when the type was a host enum, so the
interpreter and the JIT were wrong in the same way for host enums.

HX_DO_ENUM_RTTI, which Haxe expands in every generated enum, now gives
the enum _hx_isEnumValueOf, which compares the value's class with the
enum's own. CastPtr and TCanCast go through hx::IsInstanceOfType, which
uses _hx_isEnumValueOf when the type has it and the class id otherwise.
Classes, hx::EnumBase and Enum keep the check they had. Both are one
virtual call and a comparison.

EnumInstanceChecks covers typed catches, Std.isOfType, is and safe casts
with plain enums, enums with parameters and type parameters, a value
held as Dynamic and haxe.Exception or Dynamic after an enum catch.
CppiaEnumInstanceChecks checks host and script enums both ways, compiled,
interpreted and jitted. The expected output of both is what Haxe's
interpreter prints. The new benchmarks time Std.isOfType and typed
catches of enums, and typed catches of classes.

Fixes #491
HaxeFoundation/haxe#12151
The helper the cppia JIT calls to build a host enum value with arguments
read them from the script frame in place, and wrote the new value back
the same way. A frame keeps 4-byte alignment, so on 64-bit an object
slot in it can sit at an odd multiple of 4: undefined behaviour, and a
fault on a CPU that requires alignment. 7a78380 moved the JIT's helpers
to copying slots in and out and missed this one.

CppiaEnumInstanceChecks is the first test to build a host enum with an
argument from jitted code at such an offset, HostB.HostWith(1), and
UBSan stopped it in createEnum (CppiaClasses.cpp:368, load of
misaligned address). The helper now uses JitArg and JitStore, as the
others do.
@dimensionscape
dimensionscape force-pushed the fix/enum-instance-checks branch from a37515e to e19aa70 Compare November 27, 2026 15:47
@dimensionscape
dimensionscape marked this pull request as ready for review November 27, 2026 15:48
@dimensionscape
dimensionscape merged commit 9cce310 into main Nov 27, 2026
37 checks passed
dimensionscape added a commit that referenced this pull request Nov 28, 2026
#492 gave HX_DO_ENUM_RTTI _hx_isEnumValueOf, which compares a value's
class with the enum's own __mClass, and returned false when __mClass is
null. An extern enum written by hand in C++, as Haxe's
unit.issues.Issue10830 does, never registers its class, so every value
of it failed its own check: the switch in that test threw Invalid Cast,
and so would a typed catch of it or a cast to it. 6.0.2 shipped with it,
since the haxe unit job runs only on pushes to main.

An enum whose __mClass is null takes any enum value again, through the
clsIdEnum check every enum used before #492. Enums Haxe generates have
their class, and still check for their own.

EnumUnregisteredClass is Issue10830's extern enum, with a switch, a cast
from Dynamic and a typed catch, and checks that generated enums still
tell each other apart. It threw Invalid Cast before this change.

Tests: EnumUnregisteredClass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A typed catch of one enum catches a value of any enum in compiled code

1 participant